package cn.wolfcode.shop.member.web.controller.rpc;

import cn.wolfcode.shop.common.util.Result;
import cn.wolfcode.shop.member.domain.User;
import cn.wolfcode.shop.member.feign.UserFeignApi;
import cn.wolfcode.shop.member.mapper.UserMapper;
import cn.wolfcode.shop.util.BaseKeyPrefix;
import cn.wolfcode.shop.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserFeignClient implements UserFeignApi {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public Result<User> get(Long id) {

        User user = userMapper.get(id);

        return Result.success(user);
    }

    @Override
    public Result<Boolean> refreshToken(String token) {

        BaseKeyPrefix keyprefix = new BaseKeyPrefix(1800, "USER_TOKEN:");

        User user = redisService.get(keyprefix, token, User.class);

        if (user != null) {

            redisService.set(keyprefix,token,user);

            return Result.success(true);
        }

        return null;
    }

}
